package com.xxc.hs.funActivity.panel;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class AnswerDao {
    private EelFaqDatabase dbHelper;

    public AnswerDao(Context context) {
        dbHelper = new EelFaqDatabase(context);
    }

    // 根据问题ID获取解答
    public Answer getAnswerByQuestionId(int questionId) {
        Answer answer = null;
        SQLiteDatabase db = dbHelper.getReadableDatabase();

        String[] projection = {
                EelFaqDatabase.COLUMN_ANSWER_ID,
                EelFaqDatabase.COLUMN_ANSWER_QUESTION_ID,
                EelFaqDatabase.COLUMN_ANSWER_CONTENT,
                EelFaqDatabase.COLUMN_ANSWER_EXPERT_ID
        };

        String selection = EelFaqDatabase.COLUMN_ANSWER_QUESTION_ID + " = ?";
        String[] selectionArgs = {String.valueOf(questionId)};

        Cursor cursor = db.query(
                EelFaqDatabase.TABLE_ANSWERS,
                projection,
                selection,
                selectionArgs,
                null,
                null,
                null
        );

        if (cursor != null && cursor.moveToFirst()) {
            int id = cursor.getInt(cursor.getColumnIndexOrThrow(EelFaqDatabase.COLUMN_ANSWER_ID));
            int qId = cursor.getInt(cursor.getColumnIndexOrThrow(EelFaqDatabase.COLUMN_ANSWER_QUESTION_ID));
            String content = cursor.getString(cursor.getColumnIndexOrThrow(EelFaqDatabase.COLUMN_ANSWER_CONTENT));
            int expertId = cursor.getInt(cursor.getColumnIndexOrThrow(EelFaqDatabase.COLUMN_ANSWER_EXPERT_ID));

            answer = new Answer(id, qId, content, expertId);
            cursor.close();
        }

        db.close();
        return answer;
    }
}